package com.ruoyi.hotel.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.hotel.domain.HmPurchaseApply;
import com.ruoyi.hotel.domain.HmSupply;
import com.ruoyi.hotel.mapper.HmPurchaseApplyMapper;
import com.ruoyi.hotel.mapper.HmSupplyMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.hotel.domain.bo.HmSupplyPurchaseBo;
import com.ruoyi.hotel.domain.vo.HmSupplyPurchaseVo;
import com.ruoyi.hotel.domain.HmSupplyPurchase;
import com.ruoyi.hotel.mapper.HmSupplyPurchaseMapper;
import com.ruoyi.hotel.service.IHmSupplyPurchaseService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-01
 */
@RequiredArgsConstructor
@Service
public class HmSupplyPurchaseServiceImpl implements IHmSupplyPurchaseService {

    private final HmSupplyPurchaseMapper baseMapper;
    private final HmPurchaseApplyMapper hmPurchaseApplyMapper;
    private final HmSupplyMapper hmSupplyMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public HmSupplyPurchaseVo queryById(Long purchaseId){
        return baseMapper.selectVoById(purchaseId);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<HmSupplyPurchaseVo> queryPageList(HmSupplyPurchaseBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HmSupplyPurchase> lqw = buildQueryWrapper(bo);
        Page<HmSupplyPurchaseVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<HmSupplyPurchaseVo> queryList(HmSupplyPurchaseBo bo) {
        LambdaQueryWrapper<HmSupplyPurchase> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HmSupplyPurchase> buildQueryWrapper(HmSupplyPurchaseBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HmSupplyPurchase> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getWorkerId() != null, HmSupplyPurchase::getWorkerId, bo.getWorkerId());
        lqw.eq(bo.getSupplyId()!=null, HmSupplyPurchase::getSupplyId, bo.getSupplyId());
        lqw.eq(bo.getPurchaseApplyId() != null, HmSupplyPurchase::getPurchaseApplyId, bo.getPurchaseApplyId());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(HmSupplyPurchaseBo bo) {
        HmSupplyPurchase add = BeanUtil.toBean(bo, HmSupplyPurchase.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setPurchaseId(add.getPurchaseId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(HmSupplyPurchaseBo bo) {
        HmSupplyPurchase update = BeanUtil.toBean(bo, HmSupplyPurchase.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HmSupplyPurchase entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public void doPurchase(HmSupplyPurchaseBo bo,Long id){
        bo.setPurchaseTotal(bo.getPurchaseAmount()*bo.getPurchasePrice()+bo.getDeliveryFee());
        bo.setWorkerId(id);

        HmPurchaseApply hmPurchaseApply=hmPurchaseApplyMapper.selectById(bo.getPurchaseApplyId());

        bo.setPurchaseReturn(hmPurchaseApply.getPurchaseApplyMoney()-bo.getPurchaseTotal());

        HmSupply hmSupply=hmSupplyMapper.selectById(bo.getSupplyId());
        hmSupply.setSupplyAmount(hmSupply.getSupplyAmount()+bo.getPurchaseAmount());
        hmSupplyMapper.updateById(hmSupply);

        this.insertByBo(bo);

        hmPurchaseApply.setIsPass("d");
        hmPurchaseApplyMapper.updateById(hmPurchaseApply);
    }
}
